import { isEmpty, isNumber, isString } from 'microconnect-common/utils/is';
import { equal } from 'microconnect-common/utils/math';

export const newToString = (value?: any) => (value ?? '').toString();

/**
 * 姓名脱敏
 * @param value
 */
export const secertName = (value: string) => {
  if (!value) return '';
  if (value.length === 1) return '*';
  else if (value.length === 2) return '**';
  else return value.replace(/(\S{1})(\S.*)(\S{1})/, '*$2*');
};

/**
 * 给字符串指定位置打码
 * @param {string} string 需要打码字符串
 * @param {number} start 开始位置
 * @param {number} length 打码长度
 * @param {string} mask 打码符号，默认: *
 * @returns {string}
 *
 * @example
 * maskString('13878822602', 2);     // => '13*********'
 * maskString('13878822602', 3, 4);  // => '138****2602'
 * maskString('13878822602', 3, 4, 'x');  // => '138xxxx2602'
 */
export const maskString = (string?: string, start?: number, length?: number | undefined, mask?: string) => {
  const str = String(string);
  if (!isNumber(start)) return str;
  if (isString(length)) {
    mask = length;
    length = undefined;
  }
  if (!isString(mask)) mask = '*';
  const maskStr = str
    .substr(start, length)
    .replace(/[^\u4e00-\u9fa5]/gm, mask)
    .replace(/[\u4e00-\u9fa5]/gm, mask + mask);
  const prefix = str.substr(0, start);
  const suffix = length === undefined ? '' : str.substr(start + length);
  return prefix + maskStr + suffix;
};

/**
 * 阿拉伯数字转中文数字,
 * 如果传入数字时则最多处理到21位，超过21位js会自动将数字表示成科学计数法，导致精度丢失和处理出错
 * 传入数字字符串则没有限制
 * @param {number|string} digit
 */
export const toZhDigit = (digit: number | string) => {
  digit = typeof digit === 'number' ? String(digit) : digit;
  const zh = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  const unit = ['千', '百', '十', ''];
  const quot = [
    '万',
    '亿',
    '兆',
    '京',
    '垓',
    '秭',
    '穰',
    '沟',
    '涧',
    '正',
    '载',
    '极',
    '恒河沙',
    '阿僧祗',
    '那由他',
    '不可思议',
    '无量',
    '大数'
  ];

  let breakLen = Math.ceil(digit.length / 4);
  let notBreakSegment = digit.length % 4 || 4;
  let segment: any;
  let zeroFlag = [],
    allZeroFlag = [];
  let result = '';

  while (breakLen > 0) {
    if (!result) {
      // 第一次执行
      segment = digit.slice(0, notBreakSegment);
      let segmentLen = segment.length;
      for (let i = 0; i < segmentLen; i++) {
        if (!equal(segment[i], 0)) {
          if (zeroFlag.length > 0) {
            result += '零' + zh[segment[i]] + unit[4 - segmentLen + i];
            // 判断是否需要加上 quot 单位
            if (i === segmentLen - 1 && breakLen > 1) {
              result += quot[breakLen - 2];
            }
            zeroFlag.length = 0;
          } else {
            result += zh[segment[i]] + unit[4 - segmentLen + i];
            if (i === segmentLen - 1 && breakLen > 1) {
              result += quot[breakLen - 2];
            }
          }
        } else {
          // 处理为 0 的情形
          if (segmentLen === 1) {
            result += zh[segment[i]];
            break;
          }
          zeroFlag.push(segment[i]);
          continue;
        }
      }
    } else {
      segment = digit.slice(notBreakSegment, notBreakSegment + 4);
      notBreakSegment += 4;

      for (let j = 0; j < segment.length; j++) {
        if (!equal(segment[j], 0)) {
          if (zeroFlag.length > 0) {
            // 第一次执行zeroFlag长度不为0，说明上一个分区最后有0待处理
            if (j === 0) {
              result += quot[breakLen - 1] + zh[segment[j]] + unit[j];
            } else {
              result += '零' + zh[segment[j]] + unit[j];
            }
            zeroFlag.length = 0;
          } else {
            result += zh[segment[j]] + unit[j];
          }
          // 判断是否需要加上 quot 单位
          if (j === segment.length - 1 && breakLen > 1) {
            result += quot[breakLen - 2];
          }
        } else {
          // 第一次执行如果zeroFlag长度不为0, 且上一划分不全为0
          if (j === 0 && zeroFlag.length > 0 && allZeroFlag.length === 0) {
            result += quot[breakLen - 1];
            zeroFlag.length = 0;
            zeroFlag.push(segment[j]);
          } else if (allZeroFlag.length > 0) {
            // 执行到最后
            if (breakLen === 1) {
              result += '';
            } else {
              zeroFlag.length = 0;
            }
          } else {
            zeroFlag.push(segment[j]);
          }

          if (j === segment.length - 1 && zeroFlag.length === 4 && breakLen !== 1) {
            // 如果执行到末尾
            if (breakLen === 1) {
              allZeroFlag.length = 0;
              zeroFlag.length = 0;
              result += quot[breakLen - 1];
            } else {
              allZeroFlag.push(segment[j]);
            }
          }
          continue;
        }
      }

      --breakLen;
    }

    return result;
  }
};

/**
 * 格式化银行卡号
 * @param {string} string
 * @param {number} digit
 * @param {string} symbol
 */
export const bankCardFormat = (string: string, digit?: number, symbol?: string) => {
  if (isEmpty(string)) return '';

  if (!digit) digit = 4;

  if (!symbol) symbol = ' ';

  const arr = [];

  for (let i = 0, len = string.length / digit; i < len; i++) {
    let subStr = string.substr(0, digit);
    arr.push(subStr);
    string = string.replace(subStr, '');
  }

  return arr.join(symbol);
};

